from apps.common.base.views import *
from apps.pay.order.dao import OrderDao
from apps.merchant.item.dao import ItemDao
from django.db.models import Sum
from decimal import Decimal
from apps.system.lessee.dao import LesseeDao
from apps.system.user.dao import UserDao
from apps.pay.recordIncomes.dao import RecordIncomesDao

class LargeDataViewSet(BaseViewSet):
    '''总体数据'''

    @action(methods=['post'], detail=False)
    def getTotalityData(self, request):
        params = getRequestParams(request)
        data = {}
        if params.get('lessee'):
            _lessee = LesseeDao().findOneByObjId(params.get('lessee'))
            data['shopName'] = _lessee.name
            data['freePoolDay'] = str(_lessee.freePoolDay)
            data['freePoolMonth'] = str(_lessee.freePoolMonth)
            data['operationAwardPool'] = str(_lessee.operationAwardPool)
            data['supplyAwards'] = str(_lessee.supplyAwards)
            data['helpFundPool'] = str(_lessee.helpFundPool)
            data['grossProfit'] = str(_lessee.grossProfit)
        # 订单
        orders = OrderDao().findAllSubOrder()
        if BaseDao().findRoleByAccount(self.request.user).key == '供应链':
            data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND', shop__types='0').count()
            data['wait_rake_count'] = orders.filter(orderStatus='WAIT_RECEIVE', shop__types='0').count()
            # 商品总数
            items = ItemDao().findSelfItem()
            data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()

            data['stock_short_count'] = items.filter(checkStatus='PASS', status=1).filter(item_sku__is_delete=0, item_sku__stock__lt=5).count()
        elif BaseDao().findRoleByAccount(self.request.user).key == '加盟店':
            orders = orders.filter(shop__admin=self.request.user, shop__types='1')
            data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND').count()
            data['wait_rake_count'] = orders.filter(orderStatus='WAIT_RECEIVE').count()
            # 商品总数
            items = ItemDao().findFranchiseeItem().filter(shop__admin=self.request.user, shop__types='1')
            data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()
            data['stock_short_count'] = items.filter(checkStatus='PASS', status=1).filter(item_sku__is_delete=0, item_sku__stock__lt=5).count()
        else:
            data['wait_send_count'] = orders.filter(orderStatus='WAIT_SEND').count()
            data['wait_rake_count'] = orders.filter(orderStatus='WAIT_RECEIVE').count()
            # 商品总数
            items = ItemDao().findAllItem()
            data['selling_count'] = items.filter(checkStatus='PASS', status=1).count()
            data['stock_short_count'] = items.filter(checkStatus='PASS', status=1).filter(item_sku__is_delete=0, item_sku__stock__lt=5).distinct().count()
        
        # 实时数据
        #总购物积分
        users = UserDao().findAllUser()
        incomesAll = users.aggregate(sums=Sum('incomes'))['sums'] or 0
        data['incomesAll'] = str(incomesAll)
        #总锁定补贴
        incomesLocked = RecordIncomesDao().findAll().filter(payway='incomesLocked', changeWay='add', moneyLocked__gt=0).aggregate(sums=Sum('moneyLocked'))['sums'] or 0
        data['incomesLocked'] = str(incomesLocked)
        #总贡献值
        contributionValue = RecordIncomesDao().findAll().filter(payway='contributionValue', changeWay='add', money__gt=0).aggregate(sums=Sum('money'))['sums'] or 0
        data['contributionValue'] = str(contributionValue)
        #抵扣积分
        incomesCreditAll = users.aggregate(sums=Sum('incomesCredit'))['sums'] or 0
        data['incomesCreditAll'] = str(incomesCreditAll)
        #原积分
        incomesOriginalAll = users.aggregate(sums=Sum('incomesOriginal'))['sums'] or 0
        data['incomesOriginalAll'] = str(incomesOriginalAll)
        # 提现中
        outMoneying = RecordIncomesDao().findAll().filter(payway='incomes', scene='outMoney', orderStatus='WAIT_SEND').aggregate(sums=Sum('money'))['sums'] or 0
        data['outMoneying'] = str(outMoneying)
        return Response(response_success(data))
